package com.weishi.project.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.weishi.framework.web.controller.BaseController;
import com.weishi.framework.web.domain.AjaxResult;
import com.weishi.framework.web.page.TableDataInfo;
import com.weishi.project.api.ret.TrainingAllDataInfoRet;
import com.weishi.project.api.vo.TrainingDataVo;
import com.weishi.project.mcs8.Mcs8Cilent;
import com.weishi.project.training.domain.TrainingAchievement;
import com.weishi.project.training.service.ITrainingAchievementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/trainingData")
@Api(value = "TrainingData", description = "训练数据")
public class ApiTrainingDataController extends BaseController {

    @Autowired
    private ITrainingAchievementService trainingAchievementService;

    /**
     *
     * @param trainingDataVo
     * @return
     */
    @ApiOperation(value = "水上(陆上)训练数据详细查询")
    @PostMapping("/qryDetailedReportByParams")
    public AjaxResult qryDetailedReportByParams(@RequestBody TrainingDataVo trainingDataVo) {
        List<Map<String, Object>> ret = trainingAchievementService.qryDetailedReportByParams(trainingDataVo);
        return AjaxResult.success(ret);
    }

    @ApiOperation(value = "水上(陆上)训练数据详细分析报告")
    @GetMapping("/qryDetailedReportByAchievementId")
    public AjaxResult qryDetailedReportByAchievementId(@RequestParam("type") Integer type,
                                                           @RequestParam("trainingAchievementId") Integer trainingAchievementId) {
        TrainingAllDataInfoRet ret = trainingAchievementService.qryTrainingAchievementDetailedReport(trainingAchievementId,type);
        return AjaxResult.success(ret);
    }


    @ApiOperation(value = "成绩查询")
    @GetMapping("/qryTrainingAchievementInfo")
    public TableDataInfo qryTrainingAchievementInfo(TrainingAchievement trainingAchievement) {
        startPage();
        List<TrainingAchievement> list = trainingAchievementService.selectTrainingAchievementList(trainingAchievement);
        return getDataTable(list);
    }

    @ApiOperation(value = "水上(陆上)训练——分页查询训练成绩录入记录")
    @GetMapping("/list")
    public TableDataInfo list(TrainingAchievement trainingAchievement) {
        startPage();
        List<TrainingAchievement> list = trainingAchievementService.selectTrainingAchievementList(trainingAchievement);
        return getDataTable(list);
    }

    @ApiOperation(value = "水上(陆上)训练——获取训练成绩录入详细信息")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(trainingAchievementService.getById(id));
    }

    @ApiOperation(value = "水上(陆上)训练——训练成绩录入")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TrainingAchievement trainingAchievement) {
        trainingAchievement.setCtime(new Date());
        trainingAchievementService.save(trainingAchievement);
        return AjaxResult.success();
    }

    @ApiOperation(value = "水上(陆上)训练——修改训练成绩")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TrainingAchievement trainingAchievement) {
        trainingAchievementService.updateById(trainingAchievement);
        return AjaxResult.success();
    }

    @ApiOperation(value = "水上(陆上)训练——删除训练成绩")
    @GetMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        trainingAchievementService.removeByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }


    @ApiOperation("运动员端 -- 查询航道视频历史数据")
    @RequestMapping(value = "/getRaceDataList", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult getRaceDataList(@RequestBody JSONObject jsonObject) {
        Mcs8Cilent mcs8Cilent = Mcs8Cilent.getInstant();
        List<String> list = mcs8Cilent.getHistoryFileList(jsonObject.get("deviceId").toString() , jsonObject.get("stime").toString(), jsonObject.get("etime").toString(),0,10);
        if(list.size()>0){
            return AjaxResult.success("请求成功！",list.get(0));
        }else{
            return AjaxResult.success();
        }
    }




}
